#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions
cluster="monitoring"

create_infra $namespace
deploy_helm $namespace

desc 'install PMM Server'
platform=kubernetes
if [ ! -z "$OPENSHIFT" ]; then
    platform=openshift
fi
helm del monitoring || :
helm install monitoring percona/pmm-server --set platform=$platform --version 1.17.3

desc 'create PXC cluster'
spinup_pxc "$cluster" "$test_dir/conf/$cluster.yml" 3 120

desc 'check if pmm-client container enabled'
compare_kubectl statefulset/$cluster-pxc
compare_kubectl statefulset/$cluster-proxysql

desc 'check mysql metrics'
get_metric_values node_boot_time $cluster-pxc-0
get_metric_values mysql_global_status_uptime $cluster-pxc-0

desc 'check proxysql metrics'
get_metric_values node_boot_time $cluster-proxysql-0
get_metric_values proxysql_connection_pool_status $cluster-proxysql-0

# wait for QAN
sleep 90

desc 'check QAN data'
get_qan_values $cluster-pxc-0

helm delete monitoring
destroy $namespace
